package com.dianping.cat.home.group.transform;

import com.dianping.cat.home.group.IEntity;
import com.dianping.cat.home.group.IVisitor;
import com.dianping.cat.home.group.entity.Domain;
import com.dianping.cat.home.group.entity.DomainGroup;
import com.dianping.cat.home.group.entity.Group;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;










public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

  public DefaultJsonBuilder() { this(false); }



/*  33 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  37 */     this.m_compact = compact;
/*  38 */     this.m_sb = sb;
/*  39 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  43 */     indent();
/*  44 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  45 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  49 */     this.m_level--;

/*  51 */     trimComma();
/*  52 */     indent();
/*  53 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  57 */     int len = nameValues.length;

/*  59 */     for (int i = 0; i + 1 < len; i += 2) {
/*  60 */       Object attrName = nameValues[i];
/*  61 */       Object attrValue = nameValues[i + 1];

/*  63 */       if (attrValue != null) {
/*  64 */         if (attrValue instanceof Collection) {

/*  66 */           Collection<Object> items = (Collection)attrValue;

/*  68 */           if (!items.isEmpty()) {
/*  69 */             indent();
/*  70 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  72 */             for (Object item : items) {
/*  73 */               this.m_sb.append(' ');
/*  74 */               toString(this.m_sb, item);
/*  75 */               this.m_sb.append(',');
            }

            this.m_sb.setLength(this.m_sb.length() - 1);
/*  79 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  81 */         } else if (attrValue.getClass().isArray()) {
/*  82 */           int length = Array.getLength(attrValue);

/*  84 */           if (length > 0) {
/*  85 */             indent();
/*  86 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  88 */             for (int j = 0; j < length; j++) {
/*  89 */               Object item = Array.get(attrValue, j);
/*  90 */               this.m_sb.append(' ');
/*  91 */               toString(this.m_sb, item);
/*  92 */               this.m_sb.append(',');
            }

            this.m_sb.setLength(this.m_sb.length() - 1);
/*  96 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/*  99 */         } else if (this.m_compact) {
/* 100 */           this.m_sb.append('"').append(attrName).append("\":");
/* 101 */           toString(this.m_sb, attrValue);
/* 102 */           this.m_sb.append(",");
        } else {
/* 104 */           indent();
/* 105 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 106 */           toString(this.m_sb, attrValue);
/* 107 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 113 */     if (dynamicAttributes != null) {
/* 114 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 115 */         if (this.m_compact) {
/* 116 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 117 */           toString(this.m_sb, e.getValue());
/* 118 */           this.m_sb.append(","); continue;
        }
/* 120 */         indent();
/* 121 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 122 */         toString(this.m_sb, e.getValue());
/* 123 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 130 */     objectBegin(null);
/* 131 */     entity.accept(this);
/* 132 */     objectEnd(null);
/* 133 */     trimComma();

/* 135 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 139 */     this.m_sb.append('[');

/* 141 */     if (entities != null) {
/* 142 */       for (IEntity<?> entity : entities) {
/* 143 */         objectBegin(null);
/* 144 */         entity.accept(this);
/* 145 */         objectEnd(null);
      }

/* 148 */       trimComma();
    }

/* 151 */     this.m_sb.append(']');

/* 153 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 157 */     if (!this.m_compact) {
/* 158 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 159 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 165 */     indent();

/* 167 */     if (name == null) {
/* 168 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 170 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 173 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 177 */     this.m_level--;

/* 179 */     trimComma();
/* 180 */     indent();
/* 181 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected void toString(StringBuilder sb, Object value) {
/* 185 */     if (value == null) {
/* 186 */       sb.append("null");
/* 187 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 188 */       sb.append(value);
    } else {
/* 190 */       String val = value.toString();
/* 191 */       int len = val.length();

/* 193 */       sb.append('"');

/* 195 */       for (int i = 0; i < len; i++) {
/* 196 */         char ch = val.charAt(i);

/* 198 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 202 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 205 */             sb.append("\\t");
            break;
          case '\r':
/* 208 */             sb.append("\\r");
            break;
          case '\n':
/* 211 */             sb.append("\\n");
            break;
          default:
/* 214 */             sb.append(ch);
            break;
        }

      }
/* 219 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 224 */     int len = this.m_sb.length();

/* 226 */     if (this.m_compact) {
/* 227 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 228 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 231 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 232 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }



  public void visitDomain(Domain domain) {
/* 239 */     attributes(null, new Object[] { "id", domain.getId() });

/* 241 */     if (!domain.getGroups().isEmpty()) {
/* 242 */       objectBegin("groups");

/* 244 */       for (Map.Entry<String, Group> e : domain.getGroups().entrySet()) {
/* 245 */         String key = String.valueOf(e.getKey());

/* 247 */         objectBegin(key);
/* 248 */         ((Group)e.getValue()).accept(this.m_visitor);
/* 249 */         objectEnd(key);
      }

/* 252 */       objectEnd("groups");
    }
  }



  public void visitDomainGroup(DomainGroup domainGroup) {
/* 259 */     if (!domainGroup.getDomains().isEmpty()) {
/* 260 */       objectBegin("domains");

/* 262 */       for (Map.Entry<String, Domain> e : domainGroup.getDomains().entrySet()) {
/* 263 */         String key = String.valueOf(e.getKey());

/* 265 */         objectBegin(key);
/* 266 */         ((Domain)e.getValue()).accept(this.m_visitor);
/* 267 */         objectEnd(key);
      }

/* 270 */       objectEnd("domains");
    }
  }


  public void visitGroup(Group group) {
/* 276 */     attributes(null, new Object[] { "id", group.getId() });

/* 278 */     if (!group.getIps().isEmpty()) {
/* 279 */       arrayBegin("ips");

/* 281 */       for (String ip : group.getIps()) {
/* 282 */         indent();
/* 283 */         this.m_sb.append('"').append(ip).append(this.m_compact ? "\"," : "\",\r\n");
      }

/* 286 */       arrayEnd("ips");
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/group/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */